package com.fhzn.auth.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhzn.auth.entity.Resource;
import com.fhzn.auth.entity.WebResponse;
import com.fhzn.auth.service.ResourceService;
import com.fhzn.auth.tools.PageInfoUtils;
import com.fhzn.auth.web.converter.ResourceConverter;
import com.fhzn.auth.web.request.ResourceOpRequest;
import com.fhzn.auth.web.request.ResourceQueryRequest;
import com.fhzn.auth.web.vo.ResourceVO;
import com.fhzn.commons.toolkit.entity.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

/**
 * @author 老顾
 * @apiNote 资源管理
 * @since 2023/12/20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("resource")
public class ResourceController {

    private final ResourceService resourceService;

    @GetMapping("/query")
    public WebResponse<PageInfo<ResourceVO>> query(@ParameterObject ResourceQueryRequest request) {
        Page<Resource> resourcePage = resourceService.resourceWithPage(request);
        return WebResponse.success(PageInfoUtils.convert2page(resourcePage, ResourceConverter::fromResource));
    }

    @PostMapping("/add")
    public WebResponse<ResourceVO> addResource(@RequestBody ResourceOpRequest request) {
        return WebResponse.success(resourceService.addResource(request));
    }

    @PostMapping("/update")
    public WebResponse<String> updateResource(@RequestBody ResourceOpRequest request) {
        return WebResponse.success(resourceService.updateResource(request));
    }

    @PostMapping("/import")
    public WebResponse<String> importResource(@RequestBody ResourceQueryRequest request) {
        return WebResponse.success(resourceService.importResource(request));
    }

    @PostMapping("/export")
    public WebResponse<String> exportResource(@RequestBody ResourceQueryRequest request) {
        return WebResponse.success(resourceService.exportResource(request));
    }
}
